package dev.erge.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class TCPClientSample {

    public static void main(String[] args) {
        try {
            // 连接服务器
            Socket sock = new Socket("localhost", 8088);
            OutputStream os = sock.getOutputStream();
            PrintWriter pw = new PrintWriter(os);
            pw.write("你好");
            pw.flush();
            sock.shutdownOutput();
            // 接收信息
            InputStream is = sock.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String info = null;
            while ((info = br.readLine()) != null) {
                System.out.println("服务器返回:" + info);
            }
            br.close();
            is.close();
            pw.close();
            os.close();
            sock.close();
        } catch (Exception e) {
        }
    }
}
